# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=nvidia-cg-toolkit

pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.1
pkgrel=1
pkgdesc="NVIDIA Cg libraries (mingw-w64)"
arch=('any')
url="https://developer.nvidia.com/cg-toolkit"
license=("custom")
makedepends=("dos2unix" "${MINGW_PACKAGE_PREFIX}-tools" "${MINGW_PACKAGE_PREFIX}-binutils") #"${MINGW_PACKAGE_PREFIX}-innoextract"
depends=("${MINGW_PACKAGE_PREFIX}-crt")
options=('!strip' 'staticlibs')
source=("https://developer.download.nvidia.com/cg/Cg_3.1/Cg-3.1_April2012_Setup.exe")
noextract=("Cg-3.1_April2012_Setup.exe")
sha256sums=('0039b4a3d6f2d4c57eebe11e425ddb73d0b74ac917848fd97821a3a5b776ba31')

prepare() {
  cd "$srcdir"
  cp "${startdir}/Cg-3.1_April2012_Setup.exe" "$srcdir"
  innoextract "Cg-3.1_April2012_Setup.exe"
  #find . -type f -exec dos2unix {} \;
  find . -name '*glut32*' | xargs -rtl1 rm
  rm "app/include/GL/glut.h"
}

build() {
  case ${MINGW_CHOST} in
    i686*)
      cd "$srcdir/app/bin"
    ;;
    x86_64*)
      cd "$srcdir/app/bin.x64"
    ;;
  esac
  for file in `ls *.dll`; do
    ${MINGW_PREFIX}/bin/gendef $file
    ${MINGW_PREFIX}/bin/dlltool -d ${file%dll}def -l lib${file%dll}dll.a -D $file
  done
}

package() {
  cd "$srcdir/app"
  find "include/" -name '*.h' | xargs -rtl1 -I {} install -Dm644 {} "${pkgdir}${MINGW_PREFIX}/"{}

  case ${MINGW_CHOST} in
    i686*)
      cd "$srcdir/app/bin"
    ;;
    x86_64*)
      cd "$srcdir/app/bin.x64"
    ;;
  esac

  find . -name '*.dll' | xargs -rtl1 -I {} install -Dm644 {} "${pkgdir}${MINGW_PREFIX}/bin/"{}
  find . -name '*.a' | xargs -rtl1 -I {} install -Dm644 {} "${pkgdir}${MINGW_PREFIX}/lib/"{}
}
